import facecat from "facecat"
import facecat_ws from "facecat_ws"

let gPaint = new facecat.FCPaint();
/*
* 点击方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
* clicks:点击次数
*/
gPaint.onClick = function (view, firstTouch, firstPoint, secondTouch, secondPoint, clicks) {
    facecat.onClickDefault(view, firstTouch, firstPoint, secondTouch, secondPoint, clicks);
    if (view.viewType == "menuitem") {
        console.log(view.text + "=" + view.value);
    }
};

/*
* 触摸开始方法
* view:视图
* firstTouch:是否第一次触摸
* firstPoint:第一次触摸的坐标
* secondTouch:是否第二次触摸
* secondPoint:第二次触摸的坐标
*/
gPaint.onTouchBegin = function (view, firstTouch, firstPoint, secondTouch, secondPoint) {
    facecat.onTouchBeginDefault(view, firstTouch, firstPoint, secondTouch, secondPoint);	
    if(!gMenu.visible){
        gMenu.location = view.paint.touchDownPoint;
        gMenu.visible = true;
        facecat.adjustMenu(gMenu);
        facecat.invalidate(gMenu.paint);
    }
};

let that = null;
let gMenu = new facecat.FCMenu();

// index.js
// 获取应用实例
const app = getApp()
Page({
    data: {
        canvasWidth:0,
        canvasHeight:0,
        facecatStyle:"",
        facecatContent:"",
        facecatFocus:false
      },
      bindInputChange: function(e) {  
        if(gPaint.editingTextBox){
            gPaint.editingTextBox.text = e.detail.value;
            facecat.invalidateView(gPaint.editingTextBox);
            gPaint.editingTextBox = null;
            gPaint.app.setData({
                facecatStyle: "position:absolute;left:0px;top:0px;width:0px;height:0px;display:none;", 
                facecatFocus:false,
                facecatContent:""
             })
        }
      },
  touchStartCanvas: function (e) {
    facecat_ws.handleTouchStart(gPaint, e, null);
  },touchMoveCanvas: function (e) {
    facecat_ws.handleTouchMove(gPaint, e, null);
  },touchEndCanvas: function (e) {
    facecat_ws.handleTouchEnd(gPaint, e, null, null);
  },
  onLoad() {
      that = this;
    wx.createSelectorQuery().select('#myCanvas').fields({ node: true, size: true }).exec((cres) => {       
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    canvasWidth: res.windowWidth,  
                    canvasHeight: res.windowHeight
                 })
                cres[0].width = res.windowWidth;
                cres[0].height = res.windowHeight;
            }
        });   
        facecat_ws.scaleCanvas(gPaint, cres);
        //解析加载XML
        let xml = `<?xml version="1.0" encoding="utf-8"?>
        <html xmlns="facecat">
          <head>
          </head>
          <body>
            <div dock="fill">
            </div>
          </body>
        </html>
        `;
        facecat_ws.renderFaceCatWeChat(gPaint, xml, that);
        gMenu.size = new facecat.FCSize(100, 300);
        facecat.addView(gMenu, gPaint);
        for (let i = 0; i < 10; i++) {
            let menuItem = new facecat.FCMenuItem();
            menuItem.text = "菜单项" + (i + 1);
            menuItem.value = i + 1;
            facecat.addMenuItem(menuItem, gMenu);
            for (let j = 0; j < 10; j++) {
                let subMenuItem = new facecat.FCMenuItem();
                subMenuItem.text = "子菜单项" + (j + 1);
                subMenuItem.value = j + 1;
                facecat.addMenuItemToParent(subMenuItem, menuItem);
                for (let z = 0; z < 10; z++) {
                    let sunMenuItem = new facecat.FCMenuItem();
                    sunMenuItem.text = "孙菜单项" + (z + 1);
                    sunMenuItem.value = z + 1;
                    facecat.addMenuItemToParent(sunMenuItem, subMenuItem);
                }
            }
        }
    })
  }
})
